package subscriber;

import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;

@MessageDriven(activationConfig = {
		@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
		@ActivationConfigProperty(propertyName = "destination", propertyValue = "jms/ReplyQueue") })
public class ObserverOne implements MessageListener {

	@Resource(name = "jms/ConnectionFactory")
	private ConnectionFactory connectionFactory;

	@Override
	public void onMessage(Message message) {
		try {
			// Get JMS connection and session
			Connection connection = connectionFactory.createConnection();
			Session session = connection.createSession(true, 0);

			if (message instanceof TextMessage) {
				TextMessage receivedMessage = (TextMessage) message;
				// Output sent message
				System.out.println("Received Message");
				System.out.println("\tTime:       "
						+ System.currentTimeMillis() + " ms");
				System.out.println("\tMessage ID: "
						+ receivedMessage.getJMSMessageID());
				System.out
						.println("\tContents:   " + receivedMessage.getText());
			}
			session.close();
			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
